package com.itboye.ihomebank.util;

import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.custom.datepicker.wheelview.OnWheelScrollListener;
import com.itboye.ihomebank.custom.datepicker.wheelview.WheelView;
import com.itboye.ihomebank.custom.datepicker.wheelview.adapter.ArrayWheelAdapter;
import com.itboye.ihomebank.custom.datepicker.wheelview.adapter.NumericWheelAdapter;

import java.util.Calendar;

/**
 * 日期
 */
public class YuYueChooseTimePopwindow extends PopupWindow {
	private TextView open_camera, pick_image, camera_cancel_tv;
	private View cameraMenuView;
	private int mYear = 1996;
	private int mMonth = 0;
	private int mDay = 1;
	private WheelView year;
	private WheelView month;
	private WheelView day;
	private WheelView time;
	private WheelView min;
	private WheelView sec;
	Activity ac;
	onFinishedLisenter onfinishlistener;
	boolean isStart = true;

	public WheelView getYear() {
		return year;
	}

	public WheelView getMonth() {
		return month;
	}

	public WheelView getDay() {
		return day;
	}

	public WheelView getTime() {
		return time;
	}

	public WheelView getMin() {
		return min;
	}

	public WheelView getSec() {
		return sec;
	}

	//滑动完成后 date
	private int selectYear;
	private int selectMonth;
	private int selectDay;
	private Calendar c;

	@SuppressWarnings("deprecation")
	public YuYueChooseTimePopwindow(Activity context,
			final onFinishedLisenter onfinishlistener, final boolean isStart) {
		super(context);
		this.ac = context;
		this.isStart = isStart;
		this.onfinishlistener = onfinishlistener;
		c = Calendar.getInstance();
		int norYear = c.get(Calendar.YEAR);
		int curYear = mYear;
		int curMonth ;
		int curDate = c.get(Calendar.DAY_OF_MONTH);
		if (isStart){
			curMonth = c.get(Calendar.MONTH)+1;
		}else{//结束日期比开始日期少一天
			curMonth = c.get(Calendar.MONTH)+2;
		}
		int curHour=c.get(Calendar.HOUR_OF_DAY);
		int curMin=c.get(Calendar.MINUTE);
		View view = LayoutInflater.from(context).inflate(
				R.layout.popup_choosetime, null);
		year = (WheelView) view.findViewById(R.id.year);

		NumericWheelAdapter numericWheelAdapter1 = new NumericWheelAdapter(
				context, norYear-1, norYear+3);
		numericWheelAdapter1.setLabel("年");
		year.setViewAdapter(numericWheelAdapter1);
		year.setCurrentItem(1);
		year.setCyclic(false);// �Ƿ��ѭ������
		year.addScrollingListener(scrollListener);

		month = (WheelView) view.findViewById(R.id.month);

		NumericWheelAdapter numericWheelAdapter2 = new NumericWheelAdapter(
				context, 1, 12, "%02d");
		numericWheelAdapter2.setLabel("月");
		month.setViewAdapter(numericWheelAdapter2);
		month.addScrollingListener(scrollListener);
		month.setCurrentItem(curMonth-1);
		month.setCyclic(true);
		day = (WheelView) view.findViewById(R.id.day);
		initDay(norYear, curMonth, context);
		day.setCurrentItem(curDate);
		day.setCyclic(true);

		day.addScrollingListener(scrollListener);
		time = (WheelView) view.findViewById(R.id.time);
		String[] times = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
				"11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
				"21", "22", "23", "24" };
		ArrayWheelAdapter<String> arrayWheelAdapter = new ArrayWheelAdapter<String>(
				context, times);
		time.setViewAdapter(arrayWheelAdapter);
		time.setCyclic(true);
		time.addScrollingListener(scrollListener);

		min = (WheelView) view.findViewById(R.id.min);
		NumericWheelAdapter numericWheelAdapter3 = new NumericWheelAdapter(
				context, 1, 59, "%02d");
		numericWheelAdapter3.setLabel("分");
		min.setViewAdapter(numericWheelAdapter3);
		min.setCyclic(true);
		min.addScrollingListener(scrollListener);

		sec = (WheelView) view.findViewById(R.id.sec);
		NumericWheelAdapter numericWheelAdapter4 = new NumericWheelAdapter(
				context, 1, 59, "%02d");
		numericWheelAdapter4.setLabel("秒");
		sec.setViewAdapter(numericWheelAdapter4);
		sec.setCyclic(true);
		sec.addScrollingListener(scrollListener);

		year.setVisibleItems(7);// ������ʾ����
		month.setVisibleItems(7);
		day.setVisibleItems(7);
		time.setVisibleItems(7);
		min.setVisibleItems(7);
		sec.setVisibleItems(7);

        time.setVisibility(View.GONE);
		min.setVisibility(View.GONE);

		year.setCurrentItem(1);
//		month.setCurrentItem(curMonth);
		day.setCurrentItem(curDate - 1);
		time.setCurrentItem(curHour-1);
		min.setCurrentItem(curMin-1);
		sec.setVisibility(View.GONE);
		LayoutInflater layoutInflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		this.setContentView(view);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		this.setFocusable(true);
		// this.setAnimationStyle(R.style.AnimBottom);
		ColorDrawable dw = new ColorDrawable(0xb000000);
		this.setBackgroundDrawable(dw);
		this.cameraMenuView = view.findViewById(R.id.choosetime);
		cameraMenuView.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// int height = cameraMenuView.findViewById(R.id.choosetime)
				// .getTop();
				// int y = (int) event.getY();
				// if (event.getAction() == MotionEvent.ACTION_UP) {
				// if (y < height) {
				// dismiss();
				// }
				// }

				return true;
			}
		});
		TextView cancel = (TextView) view.findViewById(R.id.cancel);
		cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				dismiss();
			}
		});

		TextView ok = (TextView) view.findViewById(R.id.ok);
		ok.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (isStart)
					onfinishlistener.getBeginTime(
							(year.getCurrentItem() + c.get(Calendar.YEAR)-1) + "",
							(1 + month.getCurrentItem()) >=10 ?(1 + month.getCurrentItem())+ "":"0"+(1 + month.getCurrentItem())+"",
							(1 + day.getCurrentItem())>=10 ?(1 + day.getCurrentItem())+ "":"0"+(1 + day.getCurrentItem())+"",
							(1 + time.getCurrentItem()) + "",
							(1 + min.getCurrentItem()) + "");
				else {
					onfinishlistener.getEndTime((year.getCurrentItem() + c.get(Calendar.YEAR)-1)+ "",
							(1 + month.getCurrentItem()) >=10 ?(1 + month.getCurrentItem())+ "":"0"+(1 + month.getCurrentItem())+"",
							(1 + day.getCurrentItem())>=10 ?(1 + day.getCurrentItem())+ "":"0"+(1 + day.getCurrentItem())+"",
							(1 + time.getCurrentItem()) + "",
							(1 + min.getCurrentItem()) + "");
				}
				dismiss();
			}
		});

	}

	OnWheelScrollListener scrollListener = new OnWheelScrollListener() {
		@Override
		public void onScrollingStarted(WheelView wheel) {
			// if (isStart){
			//
			// }
		}

		@Override
		public void onScrollingFinished(WheelView wheel) {
        switch (wheel.getId()){
			case R.id.year:
				int yearItem = wheel.getCurrentItem();
                selectYear=c.get(Calendar.YEAR)-1+yearItem;
                Log.d("上个月",selectYear+""+yearItem);
                if (isStart){
                	if (selectYear>c.get(Calendar.YEAR)-1){
//						year.setCurrentItem(1);
						month.setCurrentItem(c.get(Calendar.MONTH));
						day.setCurrentItem(c.get(Calendar.DAY_OF_MONTH)-1);
					}else {
						if (c.get(Calendar.MONTH)+1==1&&c.get(Calendar.DAY_OF_MONTH)<7){ //开年签合同如20180106
							if (selectYear==c.get(Calendar.YEAR)-1){
								year.setCurrentItem(0);
								month.setCurrentItem(11);
								day.setCurrentItem(c.get(Calendar.DAY_OF_MONTH)-1-6+31);
							}else {
//							    year.setCurrentItem(1);
								month.setCurrentItem(c.get(Calendar.MONTH));
								day.setCurrentItem(c.get(Calendar.DAY_OF_MONTH)-1);
							}

						}else {
							year.setCurrentItem(1);
							month.setCurrentItem(c.get(Calendar.MONTH));
							day.setCurrentItem(c.get(Calendar.DAY_OF_MONTH)-1);
						}
					}


				}
				break;
			case R.id.month:
				int mothItem = wheel.getCurrentItem();
				selectMonth=mothItem+1;
				Log.d("上个月",selectMonth+""+(c.get(Calendar.MONTH)-1));
				if (isStart){//开始的日期
							if (year.getCurrentItem()==0){ //年份选择的是去年的话
                                if (selectMonth!=11){
                                    month.setCurrentItem(11);
                                }
								month.setCurrentItem(11);
								initDay(c.get(Calendar.YEAR)-1, 12, ac);
							}else if (year.getCurrentItem()>1){
                                initDay(selectYear,selectMonth, ac);
							}else{
								if (selectMonth==c.get(Calendar.MONTH)){//提前一月
									//不处理
									month.setCurrentItem(c.get(Calendar.MONTH)-1);
									initDay(c.get(Calendar.YEAR), c.get(Calendar.MONTH), ac);
									day.setCurrentItem(c.get(Calendar.DAY_OF_MONTH)-1-6+getSupportEndDayofMonth(c.get(Calendar.YEAR),c.get(Calendar.MONTH)+1));
									}else if (selectMonth>c.get(Calendar.MONTH)+1){//选择下一个月的话
									initDay(c.get(Calendar.YEAR), selectMonth, ac);
								}else {
										month.setCurrentItem(c.get(Calendar.MONTH));
									    initDay(c.get(Calendar.YEAR), c.get(Calendar.MONTH)+1, ac);
									    day.setCurrentItem(c.get(Calendar.DAY_OF_MONTH)-1);
									}
					}

				}
				else { //结束日期
					if (year.getCurrentItem()==c.get(Calendar.YEAR)){//当前年份
					   initDay(c.get(Calendar.YEAR), c.get(Calendar.MONTH)+1, ac);
					}else {
						initDay(selectYear,selectMonth, ac);
					}
				}
				break;
			case R.id.day:
				int dayItem = wheel.getCurrentItem();
				selectDay=dayItem+1;
				Log.d("上个月",selectDay+""+dayItem);
				Log.d("上个月",getSupportEndDayofMonth(c.get(Calendar.YEAR),c.get(Calendar.MONTH)+1)+"");
				if (isStart){
                    if (year.getCurrentItem()==0){//去年
                        if (selectDay<=c.get(Calendar.DAY_OF_MONTH)-1-6+31){
                            day.setCurrentItem(c.get(Calendar.DAY_OF_MONTH)-1-6+31);
                        }
                    }else if (year.getCurrentItem()>1){//明年以上

					}else {
                        if (month.getCurrentItem()==c.get(Calendar.MONTH)-1){//是否是上个月 是的话滑动到6天前
                            if (selectDay<=c.get(Calendar.DAY_OF_MONTH)-1-6+getSupportEndDayofMonth(c.get(Calendar.YEAR),c.get(Calendar.MONTH)+1)){
                                day.setCurrentItem(c.get(Calendar.DAY_OF_MONTH)-1-6+getSupportEndDayofMonth(c.get(Calendar.YEAR),c.get(Calendar.MONTH)+1));
                            }

                        }else {
                            if (selectDay<=c.get(Calendar.DAY_OF_MONTH)-1-6){
                                day.setCurrentItem(c.get(Calendar.DAY_OF_MONTH)-1);
                            }
                        }
                    }

				}


				break;
		}
		}
	};

	/**
	 * 
	 * @param year
	 * @param month
	 * @return
	 */
	private int getDay(int year, int month) {
		int day ;
		boolean flag = false;
		switch (year % 4) {
		case 0:
			flag = true;
			break;
		default:
			flag = false;
			break;
		}
		switch (month) {
		case 1:
		case 3:
		case 5:
		case 7:
		case 8:
		case 10:
		case 12:
			day = 31;
			break;
		case 2:
			if (flag){
				day=29;
			}else {
				day =  28;
			}
			break;
		default:
			day = 30;
			break;
		}
		return day;
	}

	private void initDay(int arg1, int arg2, Context c) {
		NumericWheelAdapter numericWheelAdapter = new NumericWheelAdapter(c, 1,
				getDay(arg1, arg2), "%02d");
		numericWheelAdapter.setLabel("日");
		day.setViewAdapter(numericWheelAdapter);
	}

	public interface onFinishedLisenter {
		public void getBeginTime(String year, String month, String day,
				String hour, String minite);

		public void getEndTime(String year, String month, String day,
				String hour, String minite);
	}


	/**
	 * 根据提供的年月获取该月份的最后一天
	 * @Description: (这里用一句话描述这个方法的作用)
	 * @Author: gyz
	 * @Since: 2017-1-9下午2:29:38
	 * @param year
	 * @param monthOfYear
	 * @return
	 */ public static Integer getSupportEndDayofMonth(int year, int monthOfYear) {
	 	Calendar cal = Calendar.getInstance();
	// 不加下面2行，就是取当前时间前一个月的第一天及最后一天
		 cal.set(Calendar.YEAR, year);
		 cal.set(Calendar.MONTH, monthOfYear);
		 cal.set(Calendar.DAY_OF_MONTH, 1);
		 cal.add(Calendar.DAY_OF_MONTH, -1);
		 return cal.get(Calendar.DAY_OF_MONTH);
	}


}
